<template>
  <div class="plotting-menu">
    <div class="plotting-menu-title">标绘库</div>
    <div class="plotting-menu-items">
      <div
        class="plotting-menu-item"
        :class="{ active: isActive(subItem.action) }"
        v-for="(subItem, subIndex) in subItems"
        :key="subIndex"
        @click="executeFunction(subItem)"
      >
        <div class="menu-item-icon">
          <IconFont class="icon" :name="subItem.icon" size="2.0833vw" />
          <!-- <img :src="subItem.icon" alt="功能图标" /> -->
        </div>
        <div class="menu-item-text">{{ subItem.text }}</div>
      </div>
    </div>
    <div class="plotting-menu-buttons">
      <button class="menu-button" @click="clearRecently">撤销</button>
      <button class="menu-button" @click="clearAll">清除全部标绘</button>
    </div>
    <div class="panel-container" v-if="currentComponent">
      <component :is="currentComponent" @close-panel="closePanel" />
    </div>
  </div>
</template>

<script setup>
import { getobjm } from '@/utils/getobjm.js'
import { ref } from 'vue'
import {
  ESImageLabel, //图片Poi
  ESPoi3D, //3DPoi
  ESPoi2D, //2DPoi
  ESHumanPoi, //人员Poi
  ESHuman, //人员
  ESCar, //车辆
  ESGltfModel, //模型
  ESTextLabel, //文字标记
  ESWidget, //部件标注
  ESGeoDiv, //自定义标注
  ESGeoLineString, //折线
  ESGeoPolygon, //多边形
  ESLocalCircle, //圆形
  ESGeoRectangle, //矩形
} from 'earthsdk3'

// 导入各种配置面板组件
import ESImageLabelPanel from './components/ESImageLabelPanel.vue'
import ESPoi3DPanel from './components/ESPoi3DPanel.vue'
import ESPoi2DPanel from './components/ESPoi2DPanel.vue'
import ESTextLabelPanel from './components/ESTextLabelPanel.vue'
import ESHumanPanel from './components/ESHumanPanel.vue'
import { useTreeStore } from '@/stores/treeStore.js'
import { usePlottingStore } from '@/stores/plottingStore.js'
const plottingStore = usePlottingStore()
const treeStore = useTreeStore()
const objm = getobjm()
const subItems = [
  {
    text: '图片Poi',
    icon: 'tppoi',
    // icon: new URL('@/assets/images/sidebar/plottingLibrary/bhTest.png', import.meta.url).href,
    action: 'createESImageLabel',
  },
  {
    text: '3DPoi',
    icon: 'poi3d',
    action: 'createESPoi3D',
  },
  {
    text: '2DPoi',
    icon: 'poi2d',
    action: 'createESPoi2D',
  },
  {
    text: '文字标注',
    icon: 'wb',
    action: 'createESTextLabel',
  },
  {
    text: '人员',
    icon: 'ren',
    action: 'createESHuman',
  },
  {
    text: '人员Poi',
    icon: 'rypoi',
    action: 'createESHumanPoi',
  },
  {
    text: '车辆',
    icon: 'che',
    action: 'createESCar',
  },
]

// 当前打开的配置面板组件
const currentComponent = ref(null)
// 当前激活的功能
const activeAction = ref(null)
// 动作与组件的映射
const actionToComponent = {
  createESImageLabel: ESImageLabelPanel,
  createESPoi3D: ESPoi3DPanel,
  createESPoi2D: ESPoi2DPanel,
  createESTextLabel: ESTextLabelPanel,
  createESHuman: ESHumanPanel,
}

// 功能执行方法
const executeFunction = (subItem) => {
  // 如果点击的是当前激活的功能，则关闭面板
  if (activeAction.value === subItem.action && currentComponent.value !== null) {
    currentComponent.value = null
    activeAction.value = null
    return
  }

  // 设置当前激活的功能
  activeAction.value = subItem.action

  switch (subItem.action) {
    case 'createESImageLabel':
      createESImageLabel()
      break
    case 'createESPoi3D':
      createESPoi3D()
      break
    case 'createESPoi2D':
      createESPoi2D()
      break
    case 'createESTextLabel':
      createESTextLabel()
      break
    case 'createESHumanPoi':
      createESHumanPoi()
      break
    case 'createESHuman':
      createESHuman()
      break
    case 'createESCar':
      createESCar()
      break
    default:
      console.log(`未知功能: ${subItem.action}`)
  }
}

// 定义emit，用于通知父组件关闭子菜单
const emit = defineEmits(['close-submenu'])

// 检查功能是否激活
const isActive = (action) => {
  return activeAction.value === action && currentComponent.value !== null
}

// 关闭配置面板
const closePanel = () => {
  currentComponent.value = null
  activeAction.value = null
  emit('close-submenu') //关闭子菜单
}

const createESImageLabel = () => {
  //屏幕渲染(随着视野始终可见) 关闭即为场景渲染(不会随视野改变大小)
  //自适应大小(随着内容撑开 true时设置size无效)
  currentComponent.value = actionToComponent['createESImageLabel']
}

const createESPoi3D = () => {
  currentComponent.value = actionToComponent['createESPoi3D']
}

const createESPoi2D = () => {
  currentComponent.value = actionToComponent['createESPoi2D']
}
const createESTextLabel = () => {
  currentComponent.value = actionToComponent['createESTextLabel']
}

const createESHuman = () => {
  currentComponent.value = actionToComponent['createESHuman']
}

const createESHumanPoi = () => {
  currentComponent.value = null
  const sceneObjectESHumanPoi = objm.createSceneObject(ESHumanPoi)
  if (!sceneObjectESHumanPoi) return
  sceneObjectESHumanPoi.poiMode = ['Diamond02']
  sceneObjectESHumanPoi.editing = true

  sceneObjectESHumanPoi.editingChanged.disposableWeakOn(() => {
    if (sceneObjectESHumanPoi && sceneObjectESHumanPoi.editing === false) {
      const position = sceneObjectESHumanPoi.position
      const a = position[0] === 0 && position[1] === 0
      const json = sceneObjectESHumanPoi.json
      objm.destroySceneObject(sceneObjectESHumanPoi)
      setTimeout(() => {
        if (!a) {
          const addTree = treeStore.createSceneObjectTreeItemFromJson(json)
          plottingStore.createPlottingData.push(addTree)
          if (addTree) treeStore.treeChange()
        }
      }, 300)
    }
  })
  emit('close-submenu') //关闭子菜单
}

const createESCar = () => {
  currentComponent.value = null
  const sceneObjectESCar = objm.createSceneObject(ESCar)
  if (!sceneObjectESCar) return
  sceneObjectESCar.mode = 'policeCar' //警车模型
  sceneObjectESCar.editing = true

  sceneObjectESCar.editingChanged.disposableWeakOn(() => {
    if (sceneObjectESCar && sceneObjectESCar.editing === false) {
      const position = sceneObjectESCar.position
      const a = position[0] === 0 && position[1] === 0
      const json = sceneObjectESCar.json
      objm.destroySceneObject(sceneObjectESCar)
      setTimeout(() => {
        if (!a) {
          const addTree = treeStore.createSceneObjectTreeItemFromJson(json)
          plottingStore.createPlottingData.push(addTree)
          if (addTree) treeStore.treeChange()
        }
      }, 300)
    }
  })
  emit('close-submenu') //关闭子菜单
}

//撤回最近一次测量
const clearRecently = () => {
  if (plottingStore.hasCreatePlottingData) {
    // 获取数组最后一项
    const recently = plottingStore.createPlottingData.pop() //1.删除数组中的最后一个元素 2.返回被删除的元素
    treeStore.destroySceneObjectTreeItem(recently)
    treeStore.treeChange()
  }
}

//清除全部测量
const clearAll = () => {
  if (!plottingStore.hasCreatePlottingData) return
  plottingStore.createPlottingData.forEach((item) => {
    if (item) treeStore.destroySceneObjectTreeItem(item)
  })
  treeStore.treeChange()
  plottingStore.createPlottingData = []
}
</script>

<style lang="scss" scoped>
.plotting-menu {
  width: 15.625vw /* 300px -> 15.625vw */;
  background-color: rgba(0, 8, 26, 0.8);
  border-radius: 0.4167vw /* 8px -> .4167vw */;
  box-shadow: 0 0 0.5208vw /* 10px -> .5208vw */ rgba(0, 195, 255, 0.5);
  border: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.6);
  padding: 0.5208vw /* 10px -> .5208vw */;
  z-index: 2;
  .plotting-menu-title {
    font-size: 0.8333vw /* 16px -> .8333vw */;
    color: #00c3ff;
    font-weight: bold;
    padding-bottom: 0.5208vw /* 10px -> .5208vw */;
    border-bottom: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.3);
  }

  .plotting-menu-items {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 0.7813vw /* 15px -> .7813vw */;
    padding: 0.5208vw /* 10px -> .5208vw */ 0;
    flex-wrap: wrap;
    max-height: 10.4167vw /* 200px -> 10.4167vw */;
    overflow: auto;
    // 自定义滚动条样式
    &::-webkit-scrollbar {
      width: 0.4167vw /* 8px -> .4167vw */; // 滚动条宽度
    }

    &::-webkit-scrollbar-track {
      background: rgba(0, 0, 0, 0.3); // 滚动条轨道背景
      border-radius: 0.2083vw /* 4px -> .2083vw */; // 轨道圆角
    }

    &::-webkit-scrollbar-thumb {
      background: rgba(0, 195, 255, 0.5); // 滚动条滑块颜色
      border-radius: 0.2083vw /* 4px -> .2083vw */; // 滑块圆角
    }
    .plotting-menu-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      gap: 0.5208vw /* 10px -> .5208vw */;
      width: 4.1667vw /* 80px -> 4.1667vw */;
      height: 4.1667vw /* 80px -> 4.1667vw */;
      cursor: pointer;
      border-radius: 0.2083vw /* 4px -> .2083vw */;

      &:hover {
        background-color: rgba(0, 195, 255, 0.2);
        .menu-item-icon {
          .icon {
            scale: 0.8;
          }
        }
      }

      &.active {
        background-color: rgba(0, 195, 255, 0.3);
        border: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.8);

        .menu-item-text {
          color: #00c3ff;
          font-weight: bold;
        }
      }

      .menu-item-icon {
        .icon {
          width: 2.0833vw /* 40px -> 2.0833vw */;
          height: 2.0833vw /* 40px -> 2.0833vw */;
          object-fit: contain;
          transition: all 0.5s;
        }
      }

      .menu-item-text {
        height: 0.8333vw /* 16px -> .8333vw */;
        line-height: 0.8333vw /* 16px -> .8333vw */;
        font-size: 0.625vw /* 12px -> .625vw */;
        color: #ffffff;
        text-align: center;
      }
    }
  }

  .plotting-menu-buttons {
    padding-top: 0.5208vw /* 10px -> .5208vw */;
    border-top: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.3);
    display: flex;
    justify-content: space-between;
    align-items: center;

    .menu-button {
      background-color: rgba(0, 195, 255, 0.2);
      border: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.6);
      color: #00c3ff;
      padding: 0.2604vw /* 5px -> .2604vw */ 0.4167vw /* 8px -> .4167vw */;
      border-radius: 0.2083vw /* 4px -> .2083vw */;
      font-size: 12px;
      cursor: pointer;

      &:hover {
        background-color: rgba(0, 195, 255, 0.4);
      }
    }
  }

  .panel-container {
    position: absolute;
    left: 16.1458vw /* 310px -> 16.1458vw */;
    bottom: 0;
  }
}
</style>
